*! version 2.0
* 21 August 2013
* NIDS 
* Merging the household questionnaire file to the individual datasets.

* Will run on all waves

*=====================================================================================================================================
* GLOBALS FOR DATA FILES AND VERSION SUFFIXES

global DataIN "\\137.158.104.21\data\Panel Public Release 2014a\Wave 3\Anon"
global DataOUT "C:\Users\01406074\Desktop"
global VersionIN "W3_Anon_V1.2"
global VersionOUT "merged"

global temp "C:\Users\01406074\Desktop"					// tempfile to hold all the working datasets, all working datasets will
																// be deleted from this folder at the completion of the do file.
																		
version 12.0													// version of Stata being used, this is needed for the rename command.

*=====================================================================================================================================

* OPENING THE INDIVIDUAL FILES, REMOVING INDIVIDUAL SPECIFIC PREFIX, REPLACING WITH WAVE SPECIFIC PREFIX AND APPENDING INDI DATA TOGETHER

* ADULT

use "$DataIN\Adult_$VersionIN.dta", clear			// opening the dataset

rename w#_a* w#*									// replacing the current prefix with a wave specific prefix
gen dataset = "Adult"								// generating a variable to indicate the source dataset

save "$temp\adult.dta", replace						// saving the modified data as a temp file

* PROXY

use "$DataIN\Proxy_$VersionIN.dta", clear			// opening the dataset

rename w#_p* w#*									// replacing the current prefix with a wave specific prefix
gen dataset = "Proxy"								// generating a variable to indicate the source dataset

save "$temp\proxy.dta", replace						// saving the modified data as a temp file

* CHILD

use "$DataIN\Child_$VersionIN.dta", clear			// opening the dataset

rename w#_c* w#*									// replacing the current prefix with a wave specific prefix
gen dataset = "Child"								// generating a variable to indicate the source dataset

append using "$temp\adult.dta"						// appending the modified adult temp file to the modified child file
append using "$temp\proxy.dta"						// appending the modified proxy temp file to the modified child file

order dataset, last									// moving the variable "dataset" to the end of the dataset

save "$temp\indi.dta", replace						// saving the adjusted data as a temp file

*-----------------------------------------------------------------------------------------------------------------------------------------

* OPENING THE HOUSEHOLD QUESTIONNAIRE AND MERGING THE INDIVIDUAL FILE CREATED ABOVE INTO IT.

use "$DataIN\HHQuestionnaire_$VersionIN.dta", clear			// opening the household roster

merge 1:m *hhid using "$temp\indi.dta"						// performing a one to many merge on hhid, between the individuals and the household questionnaire

save "$DataOUT\HHQ_Indi_$VersionOUT.dta", replace			// saving out the resulting dataset, the defined path, as per globals

*-------------------------------------------------------------------------------------------------------------------------------------------

* ERASING THE TEMP FILES

erase "$temp\adult.dta"
erase "$temp\proxy.dta"
erase "$temp\indi.dta"

*===========================================================================================================================================

/* 

The _merge variable that is generated as a result of the merge, has the following output:

_merge == 3  (matched)			// matched indicating that individuals were merged to the household perfectly	
_merge == 2  (from using)   	// indicates that there are no individuals from the generated indi temp file, that are not being merged into the household							
_merge == 1  (from master)		// these are the "Whole household deceased" households, and they do not have any individual questionnaires.

***************************************************************************

WAVE 1 _merge results

Result                           # of obs.
-----------------------------------------
not matched                             0
matched                            28,226	(_merge==3)
-----------------------------------------

***************************************************************************

WAVE 2 _merge results

Result                           # of obs.
-----------------------------------------
not matched                           111
from master                           111	(_merge==1)
from using                              0	(_merge==2)

matched                            34,098	(_merge==3)
-----------------------------------------

*************************************************************************** 

WAVE 3 _merge results

Result                           # of obs.
-----------------------------------------
not matched                           106
from master                           106	(_merge==1)
from using                              0	(_merge==2)

matched                            37,436	(_merge==3)
-----------------------------------------

*/

*end of do file
*------------------------------------------------------------------------------------------------------------------------------------------




